<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { statisticsApi } from '@/api/controller';
  import { Toastloading, ToastWarning } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserid from '@/components/serachUserid/index.vue';
  import xmDatePicker from "@/components/xmDatePicker/index.vue";
  import table2excel from 'js-table2excel';
  const action = reactive({
    total:0,
    statistics:[] as any[],
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymd'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    user_id:''
  })
  const statusType = [
    {type:0,label:'未领取'},
    {type:1,label:'已领取'},
    {type:2,label:'已过期'},
  ]
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    const { playTime:created_st, endTime:created_ed } = action.timeObj
    const res = await statisticsApi.rebate_list({
      ...listQuery,
      page:1,
      limit:action.total || 20,
      created_st,
      created_ed
    })
    loading.close()
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const list = data.map((item:any)=>{
      return {
        ...item,
        status_text:statusType.find(res=>res.type == item.status)?.label || item.status
      }
    })
    const tHeader = [
      { title: '期数ID', key: "id"},
      { title: '期数', key: "round_time"},
      { title: '统计周期', key: "week"},
      { title: '用户ID', key: "user_id"},
      { title: '用户UUID', key: "uuid"},
      { title: '用户昵称', key: "nickname"},
      { title: '用户DD值', key: "dd_value"},
      { title: '背包送出礼物钻石', key: "all_bag_value"},
      { title: '彩蛋礼物钻石', key: "all_color_gift_value"},
      { title: '奖励钻石', key: "award_diamond"},
      { title: '是否领取', key: "status_text"},
    ]
    table2excel(tHeader, list, '惊喜返利')
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:created_st, endTime:created_ed } = action.timeObj
    const res = await statisticsApi.rebate_list({
      ...listQuery,
      created_st,
      created_ed
    })
    loading.close()
    const { data=[], total=0, total_table={} } = res?.data || {}
    action.statistics = [total_table]
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      style="width: max-content;; min-height: 80px"
      class="xm-TableCenter"
      :data="action.statistics"
      border
    >
      <el-table-column width="180" prop="total_people" label="人数" />
      <el-table-column width="180" prop="total_diamond" label="奖励钻石数" />
      <el-table-column width="180" prop="total_dd" label="用户DD值" />
      <el-table-column width="180" prop="total_bag" label="背包送出礼物钻石" />
      <el-table-column width="180" prop="total_gift" label="彩蛋礼物钻石" />
      <el-table-column width="180" prop="total_user" label="领取人数" />
      <el-table-column width="180" prop="total_award" label="领取钻石数" />
    </el-table>
    <el-table
      style="margin-top:40px;"
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="期数ID" />
      <el-table-column width="100" prop="round_time" label="期数" />
      <el-table-column width="220" prop="week" label="统计周期" />
      <el-table-column width="120" prop="user_id" label="用户ID" />
      <el-table-column width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="180" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column width="120" prop="dd_value" label="用户DD值" />
      <el-table-column width="160" prop="all_bag_value" label="背包送出礼物钻石" />
      <el-table-column width="160" prop="all_color_gift_value" label="彩蛋礼物钻石" />
      <el-table-column width="160" prop="award_diamond" label="奖励钻石" />
      <el-table-column width="120" prop="status" label="是否领取" >
        <template #default="scoped">
          {{ statusType.find(res=>res.type == scoped.row.status)?.label || scoped.row.status }}
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>